/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package company.beans;

import candidate.beans.PreferencesCandidateBean;
import entities.Company;
import entities.CompanyConfiguration;
import entities.Language;
import entities.User;
import entities.UserConfiguration;
import facades.CompanyConfigurationFacade;
import facades.CompanyFacade;
import facades.LanguageFacade;
import facades.UserConfigurationFacade;
import facades.UserFacade;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.EditedEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreEditEntityEvent;
import util.Sha256Encrypter;

/**
 *
 * @author Alvaro
 */
@Named
@SessionScoped
public class PreferencesCompanyBean implements Serializable {
    
    @Inject
    @LoggedIn
    private Company company;
    @Inject
    private UserConfigurationFacade userConfigurationFacade;
    @Inject
    private CompanyConfigurationFacade companyConfigurationFacade;
    @Inject
    private UserFacade userFacade;
    @Inject
    private CompanyFacade companyFacade;
    @Inject
    private LanguageFacade languageFacade;
    @Inject
    @PreEditEntityEvent
    private Event<CompanyConfiguration> preEditCompanyConfigurationEvent;
    @Inject
    @EditedEntityEvent
    private Event<CompanyConfiguration> editCompanyConfigurationEvent;
    @Inject
    @PreEditEntityEvent
    private Event<UserConfiguration> preEditUserConfigurationEvent;
    @Inject
    @EditedEntityEvent
    private Event<UserConfiguration> editUserConfigurationEvent;
    @Inject
    @PreEditEntityEvent
    private Event<User> preEditUserEvent;
    @Inject
    @EditedEntityEvent
    private Event<User> editUserEvent;
    private Language languageCollector;
    
    
    @PostConstruct
    public void init(){
        languageCollector=getSelectedLanguage(company);
    }

    public Language getSelectedLanguage(Company c){
        if(c.getIdUser().getUserConfiguration()!=null){
            return c.getIdUser().getUserConfiguration().getLanguage();
        }
        else{
            Language language=languageFacade.find(FacesContext.getCurrentInstance().getViewRoot().getLocale());
            if(language==null){
                language=languageFacade.getEnglish();
            }
            return language;
        }
    }
    
    public void save(){
        preEditUserConfigurationEvent.fire(company.getIdUser().getUserConfiguration());
        company.getIdUser().getUserConfiguration().setLanguage(languageCollector);
        userConfigurationFacade.edit(company.getIdUser().getUserConfiguration());
        editUserConfigurationEvent.fire(company.getIdUser().getUserConfiguration());
        preEditCompanyConfigurationEvent.fire(company.getCompanyConfiguration());
        companyConfigurationFacade.edit(company.getCompanyConfiguration());
        editCompanyConfigurationEvent.fire(company.getCompanyConfiguration());
        FacesContext context=FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        String successMessage=rb.getString("UpdatedSuccessfully");
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, successMessage, successMessage);  
        context.addMessage(null, message); 
        context.renderResponse();
        //return "/views/company/ConfigurationCompanyView?faces-redirect=true";
    }
    
      public void changePassword(){
        preEditUserEvent.fire(company.getIdUser());
        try {
            company.getIdUser().setPassword(Sha256Encrypter.getSha256(company.getIdUser().getPassword()));
            userFacade.edit(company.getIdUser());
            editUserEvent.fire(company.getIdUser());
            FacesContext context=FacesContext.getCurrentInstance();
            Locale locale=context.getViewRoot().getLocale();
            ResourceBundle.clearCache();
            ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
            String successMessage=rb.getString("UpdatedSuccessfully");
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, successMessage, successMessage);  
            context.addMessage(null, message); 
            context.renderResponse();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PreferencesCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PreferencesCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    /**
     * @return the company
     */
    public Company getCompany() {
        return company;
    }

    /**
     * @param company the company to set
     */
    public void setCompany(Company  company) {
        this.company = company;
    }

    /**
     * @return the languageCollector
     */
    public Language getLanguageCollector() {
        return languageCollector;
    }

    /**
     * @param languageCollector the languageCollector to set
     */
    public void setLanguageCollector(Language languageCollector) {
        this.languageCollector = languageCollector;
    }
}
